const express = require('express')
const app = express()
//图片读存模块
const multer = require('multer');
const fs = require('fs');
const path = require('path')
/*
*上传文件
*/
const upload = multer({ dest: './public/upload' })//实例化 dest为接受文件的文件夹
app.post('/postimage',upload.any(),(req,res) => {
	let oldname = req.files[0].path //获取path: 
	let newname = req.files[0].path + path.parse(req.files[0].originalname).ext //.jpg
	fs.renameSync(oldname, newname)
	let nowurl = 'http://localhost:3000/static/upload/'+req.files[0].filename+path.parse(req.files[0].originalname).ext
	//返回给浏览器一些信息 一个磁盘地址
	res.send({
		url:nowurl
	}); 
})
const upload1 = multer({ dest: './public/logoimg' })//实例化 dest为接受文件的文件夹
app.post('/postimage1',upload1.any(),(req,res) => {
	let oldname = req.files[0].path //获取path: 
	let newname = req.files[0].path + path.parse(req.files[0].originalname).ext //.jpg
	fs.renameSync(oldname, newname)
	let nowurl = 'http://localhost:3000/static/logoimg/'+req.files[0].filename+path.parse(req.files[0].originalname).ext
	//返回给浏览器一些信息 一个磁盘地址
	res.send({
		url:nowurl
	}); 
})
/*
*删除文件
*/
// app.delete('/delectimage',upload.any(),(req,res) => {
// 	var filePath = req.body.path;
// 	console.log("error")
// 	 try {
// 	    fs.unlinkSync(filePath)
// 	    // 重定向到列表页
// 	    res.send('删除成功')
// 	} catch (error) {
// 		console.log(error)
// 	    res.send('删除失败')
// 	}
// })
module.exports = app;
